//scanf缓冲区清理问题 
//#include<stdio.h>
// #include<ctype.h>
// int main()
// {
//    double tocal = 0;
//    int count = 0;
//    char answer = 0;
//    double value = 0;

//    for ( ;;)
//    {
//       printf("Enter a nubmer: ");
//       scanf("%lf",&value);
//       tocal += value;
//       ++count;

//       printf("Do you want to continue?(Y/N)");
//       scanf(" %c",&answer);
//         // while(getchar() != '\n');
//     // while((answer = getchar()=='\n'));
//       if(tolower(answer=='n'))
//         break;
        
         
//    }
//       printf("The anverage is: %.2lf\n",tocal/count);

//    return 0;
// }

// #include<stdio.h>
// int main()
// {
//    char a=0;
//    double number=0;
//     for(;;)
//     {
//         scanf("%lf",&number);


//         scanf("%c",&a);
//         if(a=='c')
//         break;
//     }
//     return 0;
// }

//scanf缓冲区清理问题
// #include<stdio.h>
// int main()
// {
//     double number1 = 0;
//     double number2 = 0;
//     char opertor = 0;
    

//     printf("Please enter an number:\n");
//     loop_start:
//     scanf("%lf %c %lf",&number1,&opertor,&number2);

//     switch(opertor)
//      {
//         case '+':
//         printf("=%lf\n",number1 + number2);
//         break;
//         case '-':
//         printf("=%lf\n",number1 - number2);
//         break;
//         case '*':
//         printf("=%lf\n",number1 * number2);
//         break;
//         case '/':
//         if(number2==0)
//         {
//         printf("The divisor not be 0\nPlease re-enter:\n");
//         goto loop_start;
        
//         }
//         else{printf("=%lf\n",number1 / number2);}
       
        //没有下面的break，就会打印4/2的同时跳转goto
        // break;
        // default:
        //getchar()!='\n';可以将\n他之前的字符和换行符一起清除
        // while(getchar() != '\n');
//         printf("Illegal character\nPlease re-enter:\n");
//         goto loop_start;
      
//      }
//     return 0;
// }

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    //初始化换随机数、随机数限制数、猜测数、次数
    int chosen=0;
    int limit=20;
    int guess=0;
    int count=3;

    //生成随机数序列
    srand(time(NULL));
    chosen=1+rand()%limit;

    printf("\nThis is a guessing game.");
    printf("\nI'm thinking of a number between 1 and 20");

    for(;count > 0;count--)
    {   
        //输入猜测数
        printf("\nyou have %d tr%s lsft",count,count>1?"ies":"y");
        printf("\nPlease enter your guess:");
        scanf("%d",&guess);


        //情况判断      
        if(guess==chosen)
        {
            printf("\nCongratulations,you guessed it.!\n");
            return 0;
        }
        else if(guess<1||guess>20)
        printf("\nPlease enter a number between 1 and 20\n");
        else
        printf("\nSorry,your %d guess is worng.My number is %s than that\n",count,guess<chosen?"larger":"smaller");

    }
        printf("\nYour have had three tries and failed.The number was %d\n",chosen);
    return 0;
}

